package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by Lemuel on 2016/3/30.
 */
public final class ApiDaojuUtil {
    private ApiDaojuUtil() {

    }

    public static final String DAOJU_ADD = Configuration.getString("daoju.add.url");
    public static final String DAOJU_LINK = Configuration.getString("daoju.link.url");
    public static final String DAOJU_UPDATE_STARLIST = Configuration.getString("daoju.update.starlist.url");
    public static final String DAOJU_GET_STARLIST = Configuration.getString("daoju.get.startlist.url");
    public static final String SECRET_KEY = Configuration.getString("daoju.secret.key");
    public static final String DAOJU_IP = Configuration.getString("daoju.ip");
    public static final String API_KEY = "epg";
    public static final String DAOJU_LIVE_TYPE_FREE = "0";
    public static final String DAOJU_LIVE_TYPE_PAID = "3";

    public static final String DAOJU_LINK_STATUS_ON = "1";
    public static final String DAOJU_LINK_STATUS_OFF = "2";

    public static JSONObject addDaoju(long qipuId , String adminUsername, String programName, String description,
                                  String startPlayTime, String stopPlayTime, String roomId, String pLiveType,
                                  String isGroup, String starList) {
        Map<String, String> map = new HashMap<>();
        map = fillCommonParams(map);
        map.put("link_id", qipuId + "");
        map.put("admin_username", adminUsername);
        map.put("partner_name", programName);
        map.put("partner_intro", description);
//        map.put("is_group", "");
        map.put("room_id", roomId);
        map.put("partner_live_type", pLiveType);
        map.put("online_time", startPlayTime);
        map.put("offline_time", stopPlayTime);
        map.put("is_group", isGroup);
        map.put("star_list", starList);
        map.put("sign", sign(map, SECRET_KEY));
        JSONObject result = ApiHttpRequestUtil.doPost(DAOJU_ADD ,null , map);
        if (result != null && Constants.CODE_SUC.equals(result.getString("code"))) {
            return result.getJSONObject("data");
        }
        return null;
    }

    public static int linkDaoju(long qipuId, String adminUsername, String daojuId,
                                String startPlayTime, String stopPlayTime, String roomId, String pLiveType,
                                String status, String isGroup) {
        Map<String, String> map = new HashMap<>();
        map = fillCommonParams(map);
        map.put("link_id", qipuId + "");
        map.put("partner_code", daojuId);
        map.put("admin_username", adminUsername);
//        map.put("partner_name", program_name);
//        map.put("partner_intro", "");
//        map.put("is_group", "");
        map.put("room_id", roomId);
        map.put("status", status);
        map.put("partner_live_type", pLiveType);
        map.put("online_time", startPlayTime);
        map.put("offline_time", stopPlayTime);
        if (StringUtils.isNotBlank(isGroup)) {
            map.put("is_group", isGroup);
        }
        map.put("sign", sign(map, SECRET_KEY));
        JSONObject result = ApiHttpRequestUtil.doPost(DAOJU_LINK ,null , map);
        if (result != null && Constants.CODE_SUC.equals(result.getString("code"))) {
            return 0;
        }
        return -1;
    }

    public static String updateStarList(String daojuId, String starList) {
        Map<String, String> map = new HashMap<>();
        map.put("star_list", starList);
        map.put("partner_code", daojuId);
        map.put("sign", sign(map, SECRET_KEY));
        JSONObject result = ApiHttpRequestUtil.doPost(DAOJU_UPDATE_STARLIST ,null , map);
        return result.toJSONString();
    }

    public static JSONObject getStarList(String daojuId) {
        Map<String, String> map = new HashMap<>();
        map.put("partner_code", daojuId);
        map.put("sign", sign(map, SECRET_KEY));
        JSONObject result = ApiHttpRequestUtil.doPost(DAOJU_GET_STARLIST, null, map);
        if (result != null && Constants.CODE_SUC.equals(result.getString("code"))) {
            return result.getJSONObject("data");
        }
        return null;
    }

    public static Map<String, String> fillCommonParams(Map<String, String> map) {
        if (map == null) return null;
        map.put("api_key", API_KEY);
        map.put("time", System.currentTimeMillis()/1000L + "");
        map.put("format", "json");
        map.put("version", "1.0.0");
        map.put("ip", DAOJU_IP);
        return map;
    }


    public static String sign(Map<String, String> params, String secretKey) {
        SortedMap<String, String> sortedParams = new TreeMap<String, String>(params);
        sortedParams.remove("sign");
        StringBuilder sb = new StringBuilder();
        for (String key : sortedParams.keySet()) {
            String val = sortedParams.get(key);
            sb.append(key).append(StringUtils.defaultIfEmpty(val, ""));
        }
        return DigestUtils.sha1Hex(sb.append(secretKey).toString());
    }


    public static String convertToGBK(String utfString){
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;

        while((i=utfString.indexOf("\\u", pos)) != -1){
            sb.append(utfString.substring(pos, i));
            if(i+5 < utfString.length()){
                pos = i+6;
                sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));
            }
        }

        return sb.toString();
    }


}
